package com.example.demo.controller.tovip;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.controller.BaseController;
import com.example.demo.domain.AjaxResult;
import com.example.demo.domain.tovip.Tovip;
import com.example.demo.service.tovip.TovipService;
import com.example.demo.web.TableDataInfo;



/***
 * @author zc
 */
@Controller
@RequestMapping("/tovip")
public class TovipController extends BaseController {
	@Autowired
	private TovipService tovipService;

	@RequestMapping("/")
	public String index() {

		return "index";
	}
    //加载服务管理的页面
	@GetMapping("")
	public String tovip() {
		return "tovip/tovip";
	}

	//查询方法，并将结果渲染到表格中
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Tovip tovip) {
		startPage();
		List<Tovip> list = tovipService.selectTovipList(tovip);
		return getDataTable(list);
	}
	
	@GetMapping("/add")
	public String add() {
		return  "tovip"+"/add";
	}
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult add(Tovip tovip) {
		return toAjax(tovipService.insert(tovip));
	}
	
	/**
	 * 修改
	 * 
	 * 
	 */
	//从前端获取选中的信息
	@GetMapping("/edit/{tovipId}")
	public String edit(@PathVariable("tovipId") Integer tovipId, ModelMap mmap) {
	Tovip	tovip = tovipService.selectByPrimaryKey(tovipId);
		mmap.put("tovip", tovip);
		return  "tovip/edit";
	}
	
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Tovip tovip) {
		
		return toAjax(tovipService.updateByPrimaryKey(tovip));
	}

	/**
	 * 删除广告
	 */
	
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		System.out.println(ids);
		// 查询每个id下面否有服务，如果有不允许占用
		String[] id = ids.split(",");
		int flag = 0;
		for (int i = 0; i < id.length; i++) {
			Integer tovipId = Integer.parseInt(id[i]);
			if (tovipService.selectCountByTovipId(tovipId) > 0) {
				flag = 1;
				return error(1, "所选的会员卡类型已被使用,不允许删除！");
			}
			if (flag == 1) {
				break;
			}
		}
		return toAjax(tovipService.deleteTovipByIds(id));
	}
	
	/**
	 * 校验会员卡类型名称
	 */
	@PostMapping("/checkTovipNameUnique")
	@ResponseBody
	public int checkTovipNameUnique(Tovip tovip) {
		return tovipService.checkTovipNameUnique(tovip);
	}

}
